home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Periodicals / develop / develop 6 code / Threads / Threads Package 2.0d17 / ThreadsIntf.p < prev   
Encoding:
Text File  |  1991-10-10  |  9.4 KB  |  252 lines  |  [TEXT/MPS ]

  1. unit ThreadsIntf;
  2.  
  3. interface
  4.  
  5. const
  6.  
  7.      kDefaultStackSize = 1024;
  8.      kUsesFPU = 1;
  9.      kMainThreadHasPriority = 2;
  10.     kDreamEveryTick = 4;
  11.  
  12. { Thread errors }
  13.  
  14.     threadsNotInitedErr = -2700;
  15.     badThreadErr = -2701;
  16.     threadsNotAvailableErr = -2702;
  17.     wrongThreadsVersErr = -2702;
  18.     badSemaphoreErr = -2703;
  19.     threadQueueNotEmptyErr = -2704;
  20.     badSwapSelectorErr = -2705;
  21.     noOtherThreadsToRunErr = -2706;
  22.     kSemaphoreNotFoundErr = -2712;
  23.     
  24. { Customizable swapping behavior instalation selectors }
  25.  
  26.     kCopyContextSel = 'cctx';
  27.     kSwapInSel = 'swpi';
  28.     kSwapOutSel = 'swpo';
  29.     kFreeThreadSel = 'tfre';
  30.     kScheduleSel = 'schd';
  31.     kPreYieldSel = 'prey';
  32.     kPostYieldSel = 'post';
  33.  
  34. { Thread States }
  35.  
  36.     running = 0;     { the Thread is the active context                                     }
  37.     pending = 1;    { the Thread is awaiting scheduleing ( ie . on gPendingQueue )    }
  38.     blocked = 2;    { the Thread is on a semaphore                                         }
  39.     sleeping = 3;    { the Thread is not blocked or pending                                 }
  40.     ended = 4;     { the Thread is about to be free 'd                                     }
  41.  
  42.     kNumberOfUserLongs = 8;
  43.     kNumberOfInternalLongs = 4;
  44.  
  45. { ----------------------------------------------------------- }
  46.  
  47.  type
  48.  
  49. {  Each thing can be a member of a list , and can also contain a list of other things . }
  50.  
  51.     ThingHandle = ^ThingPtr;
  52.     ThingPtr = ^Thing;
  53.     Thing = record
  54.         fQueue: ThingHandle;    { the containing “Thing list” this thing is on                  }
  55.         fNext: ThingHandle;    { the next thing in the list relative to this thing              }
  56.         fPrev: ThingHandle;    { the previous thing in the list relative to this thing          }
  57.         fHead: ThingHandle;    { the first thing in the list of things owned by this thing    }
  58.         fTail: ThingHandle;    { the last thing in the list of things owned by this thing     }
  59.     end;
  60.  
  61.     ThreadType = longInt;
  62.     ThreadState = longInt;
  63.     ThreadQueueHandle = ^ThreadQueuePtr;
  64.     ThreadQueuePtr = ^ThreadQueue;
  65.     ThreadQueue = ptr;
  66.     ThingIterProc = ProcPtr;
  67.     ThreadIterProc = ProcPtr;
  68.  
  69.  
  70.     ThreadProc = ProcPtr;
  71.     ScheduleProc = ProcPtr;
  72.     SpawnProc = ProcPtr;
  73.  
  74.     ThreadProcTbl = record
  75.         fCopyContext: ThreadProc;        { copy current context - store in fStack                     }
  76.         fSwapIn: ThreadProc;        { called to context switch "this" thread in                  }
  77.         fSwapOut: ThreadProc;        { calls fSchedule then fSwapIn on the nextThread             }
  78.         fFree: ThreadProc;        { called to dispose of the Thread                             }
  79.         fSchedule: ScheduleProc;    { queue this thread (if necessary), return the next one    }
  80.         fPreYield: ThreadProc;
  81.         fPostYield: ThreadProc;
  82.     end;
  83.  
  84.     ThreadHandle = ^ThreadPtr;
  85.     ThreadPtr = ^Thread;
  86.     Thread = record
  87.         fThing: Thing;                    { linked list for queue thread is on }
  88.         fNext: ThreadHandle;            { linked list of all threads }
  89.         fPrev: ThreadHandle;
  90.         fUserBytes: array[1..8] of longInt;    {  for user use }
  91.         fInternalUse: Ptr;                { used to keep track of internal globals }
  92.         fThreadIdentifier: longInt;                { used for error checking }
  93.         fDream: ThreadProc;                { idle proc that gets called for each thread when LetThreadsDream is called  }
  94.         fThreadProcs: ThreadProcTbl;            { customizable routines  }
  95.         fType: ThreadType;
  96.         fState: ThreadState;
  97.         fLocked: boolean;                { If the ThreadHandle and fStack are locked - avoids copious calls to HLock/HUnlock }
  98.         fStack: Handle;                    { the storage for the stack data - if ThreadType == HeapStack this is HLock'ed }
  99.     end;
  100.  
  101.     SemaphoreHandle = ^SemaphorePtr;
  102.     SemaphorePtr = ^Semaphore;
  103.     Semaphore = record
  104.         fThing: Thing;
  105.         fCount: longInt;
  106.         fSemaphoreGID: longInt;
  107.         fNext: SemaphoreHandle;
  108.         fPrev: SemaphoreHandle;
  109.         fSemaphoreIdentifier: longInt;
  110.     end;
  111.  
  112. { ----------------------------------------------------------- }
  113.  
  114.  var
  115.  
  116.     {$J+}
  117.   gThreadError: OSErr;
  118.     {$J-}
  119.  
  120. { ----------------------------------------------------------- }
  121.  
  122. { Manager Routines }
  123.  
  124.     function InitThreads(threadFlags: integer): OSErr;
  125.     function ThreadError: OSErr;
  126.     function InstallSwapProc(selector: longint; newSwap: ThreadProc): ThreadProc;
  127.     function RegisterContextGlobal(var theGlobal: longInt): OSErr;
  128.     function SetMainThread(mainThread: ThreadHandle): OSErr;
  129.     function GetCurrentThread: ThreadHandle;
  130.     procedure Yield;
  131.     procedure ExitThreads;    { while gPendingQueue is not empty Yield }
  132.  
  133. { Thread Routines }
  134.  
  135.     function NewThread(stackSize: longInt): ThreadHandle;
  136.     function StartThread(theThread: ThreadHandle): OSErr;    { call fCopy proc and wake }
  137.     function InThread(theThread: ThreadHandle): boolean;    { true if theThread is running }
  138.     function InMainThread: boolean;    { true if mainThread is running }
  139.     function Sleep(theThread: ThreadHandle): OSErr;        { remove form gPendingQueue (or swap out if running) place on gSleepingQueue }
  140.     function SleepForNTicks(theThread: ThreadHandle; sleepTime: longint): OSErr; { like sleep, but wake up after N ticks }
  141.     function Wake(theThread: ThreadHandle): OSErr;            { place on gPendingQueue }
  142.     function EndThread(theThread: ThreadHandle): OSErr;    { call fFree - never returns, end of context }
  143.  
  144. { Convenience Routines }
  145.  
  146.     function InNewThread(var theThread: ThreadHandle; stackSize: longInt): boolean;    { NewThread, StartThread, InThread }
  147.     function Spawn(theThread: ThreadHandle; theSpawnProc: SpawnProc; stackSize, refCon: longInt): ThreadHandle;
  148.  
  149. { Default methods }
  150.  
  151.     procedure TCopyContext(theThread: ThreadHandle);    { copy the current context }
  152.     procedure TSwapIn(theThread: ThreadHandle);        { make this thread's context the active context }
  153.     procedure TSwapOut(theThread: ThreadHandle);        { save the current context (or free the thread if fState == ended ) }
  154.     procedure TFree(theThread: ThreadHandle);            { dispose of the thread }
  155.     function TSchedule(theThread: ThreadHandle): ThreadHandle;    { returns the next thread to be run, queue this if necessary }
  156.  
  157. { Thing Routines }
  158.  
  159.     procedure IThing(theThing: ThingHandle);
  160.     procedure ThingInsertLast(queue, thing: ThingHandle);
  161.     function ThingTakeFirst(theThing: ThingHandle): ThingHandle;
  162.     function ThingGetFirst(theThing: ThingHandle): ThingHandle;
  163.     procedure ThingRemove(theThing: ThingHandle);
  164.     function ThingOwner(theThing: ThingHandle): ThingHandle;
  165.     function ThingIsEmpty(theThing: ThingHandle): boolean;
  166.     procedure ThingEach(q: ThingHandle; proc: ThingIterProc; refCon: ptr);
  167.     procedure FreeThing(theThing: ThingHandle);
  168.  
  169. { Dreaming Support }
  170.  
  171.     procedure ThreadEach(theProc: ThreadIterProc; refCon: ptr);
  172.     procedure LetThreadsDream;
  173.  
  174. { Semaphores }
  175.  
  176.     function NewSemaphore: SemaphoreHandle;
  177.     procedure BlockOnSemaphore(theSemaphore: SemaphoreHandle; theThread: ThreadHandle);
  178.     procedure ReleaseOneThread(theSemaphore: SemaphoreHandle);
  179.     procedure ReleaseAllThreads(theSemaphore: SemaphoreHandle);
  180.     procedure FreeSemaphore(theSemaphore: SemaphoreHandle);
  181.     procedure GrabSemaphore(theSemaphore: SemaphoreHandle);
  182.     procedure ReleaseSemaphore(theSemaphore: SemaphoreHandle);
  183.     function LookupSemaphore(semaphoreGID: longInt): SemaphoreHandle;
  184.  
  185. { ----------------------------------------------------------- }
  186.  
  187. implementation
  188.  
  189. { Manager Routines }
  190.  
  191.     function InitThreads(threadFlags: integer): OSErr; external;
  192.     function ThreadError: OSErr; external;
  193.     function InstallSwapProc(selector: longint; newSwap: ThreadProc): ThreadProc; external;
  194.     function RegisterContextGlobal(var theGlobal: longInt): OSErr; external;
  195.     function SetMainThread(mainThread: ThreadHandle): OSErr; external;
  196.     function GetCurrentThread: ThreadHandle; external;
  197.     procedure Yield; external;
  198.     procedure ExitThreads; external;
  199.  
  200. { Thread Routines }
  201.  
  202.     function NewThread(stackSize: longInt): ThreadHandle; external;
  203.     function StartThread(theThread: ThreadHandle): OSErr; external;
  204.     function InThread(theThread: ThreadHandle): boolean; external;
  205.     function InMainThread: boolean; external;
  206.     function Sleep(theThread: ThreadHandle): OSErr; external;
  207.     function SleepForNTicks(theThread: ThreadHandle; sleepTime: longint): OSErr; external;
  208.     function Wake(theThread: ThreadHandle): OSErr; external;
  209.     function EndThread(theThread: ThreadHandle): OSErr; external;
  210.  
  211. { Convenience Routines }
  212.  
  213.     function InNewThread(var theThread: ThreadHandle; stackSize: longInt): boolean; external;
  214.     function Spawn(theThread: ThreadHandle; theSpawnProc: SpawnProc; stackSize, refCon: longInt): ThreadHandle; external;
  215.  
  216. { Default methods }
  217.  
  218.     procedure TCopyContext(theThread: ThreadHandle); external;
  219.     procedure TSwapIn(theThread: ThreadHandle); external;
  220.     procedure TSwapOut(theThread: ThreadHandle); external;
  221.     procedure TFree(theThread: ThreadHandle); external;
  222.     function TSchedule(theThread: ThreadHandle): ThreadHandle; external;
  223.  
  224. { Thing Routines }
  225.  
  226.     procedure IThing(theThing: ThingHandle); external;
  227.     procedure ThingInsertLast(queue, thing: ThingHandle); external;
  228.     function ThingTakeFirst(theThing: ThingHandle): ThingHandle; external;
  229.     function ThingGetFirst(theThing: ThingHandle): ThingHandle; external;
  230.     procedure ThingRemove(theThing: ThingHandle); external;
  231.     function ThingOwner(theThing: ThingHandle): ThingHandle; external;
  232.     function ThingIsEmpty(theThing: ThingHandle): boolean; external;
  233.     procedure ThingEach(q: ThingHandle; proc: ThingIterProc; refCon: ptr); external;
  234.     procedure FreeThing(theThing: ThingHandle); external;
  235.  
  236. { Dreaming Support }
  237.  
  238.     procedure ThreadEach(theProc: ThreadIterProc; refCon: ptr); external;
  239.     procedure LetThreadsDream; external;
  240.  
  241. { Semaphores }
  242.  
  243.     function NewSemaphore: SemaphoreHandle; external;
  244.     procedure BlockOnSemaphore(theSemaphore: SemaphoreHandle; theThread: ThreadHandle); external;
  245.     procedure ReleaseOneThread(theSemaphore: SemaphoreHandle); external;
  246.     procedure ReleaseAllThreads(theSemaphore: SemaphoreHandle); external;
  247.     procedure FreeSemaphore(theSemaphore: SemaphoreHandle); external;
  248.     procedure GrabSemaphore(theSemaphore: SemaphoreHandle); external;
  249.     procedure ReleaseSemaphore(theSemaphore: SemaphoreHandle); external;
  250.     function LookupSemaphore(semaphoreGID: longInt): SemaphoreHandle; external;
  251.  
  252. end.